﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Watson.Core.Tools;
using ITOMMS.Entity;
using System.Text;
using ITOMMS.Business;

namespace ITOMMS.Web.InsideManage
{
    public partial class WorkLogLisgt : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (SessionHelper.UserRoleGroup == PubEnums.RoleGroup.技术主管.ToString())
                {
                    DropDownListHelper.InitDropList<tbUserEntity>(new tbUserEntity().GetAllByQuery("(RoleGroup=" + (int)PubEnums.RoleGroup.技术主管 + " or RoleGroup=" + (int)PubEnums.RoleGroup.技术员 + ") and RoleGroup<>" + (int)PubEnums.RoleGroup.客户), this.ddlUserName, "RealName", "RealName");//绑定员工姓名
                }
                else if (SessionHelper.UserRoleGroup == PubEnums.RoleGroup.技术员.ToString())
                {
                    DropDownListHelper.InitDropList<tbUserEntity>(new tbUserEntity().GetAllByQuery("UserName='" + SessionHelper.UserName+"'"), this.ddlUserName, "RealName", "RealName");//绑定员工姓名
                    this.ddlUserName.SelectedValue = SessionHelper.RealName;
                    this.ddlUserName.Enabled = false;
                }
                else
                {
                    DropDownListHelper.InitDropList<tbUserEntity>(new tbUserEntity().GetAllByQuery("RoleGroup<>" + (int)PubEnums.RoleGroup.客户), this.ddlUserName, "RealName", "RealName");//绑定员工姓名
                }

                DropDownListHelper.InitDropList<tbDepartmentEntity>(new tbDepartmentEntity().GetAll(), ddl部门, "ID", "DepartmentName");
                //this.ddl部门.SelectedValue = SessionHelper.Department.ToString();

                //txt日期.Text = DateTime.Now.ToString("yyyy-MM-dd");

                DataShow();

                if (SessionHelper.UserRoleGroup != PubEnums.RoleGroup.管理员.ToString())
                {
                    //DataControlField colEdit = (DataControlField)this.dgdaoim_sus_plyedr.Columns[9];
                    //colEdit.Visible = false;

                    DataControlField colDelete = (DataControlField)this.dgdaoim_sus_plyedr.Columns[10];
                    colDelete.Visible = false;
                }
            }
        }


        private void DataShow()
        {
            Vw工作日志视图Entity entity = new Vw工作日志视图Entity();
            AspNetPager1.RecordCount = entity.GetTotalRecords();
            this.dgdaoim_sus_plyedr.DataSource = entity.GetAllByQuery(GetSearchSql(), "order by 日期 desc", AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex);
            //this.dgdaoim_sus_plyedr.DataSource = GetSearchSql();
            dgdaoim_sus_plyedr.DataBind();
        }

        private string GetSearchSql()
        {
            StringBuilder sb = new StringBuilder();
            //sb.Append("select RealName,日期,max(id) as id,max(DepartmentName) as DepartmentName,max(客户名称) as 客户名称,max(接报时间) as 接报时间,max(到场时间) as 到场时间,max(消耗时间) as 消耗时间 from Vw_工作日志视图");
            //sb.Append("select * from Vw_工作日志视图");
            sb.Append("  1=1");
            if (SessionHelper.UserRoleGroup == PubEnums.RoleGroup.管理员.ToString())
            {
                sb.Append(" and RoleGroup !=" + PubEnums.RoleGroup.客户.ToString("D") + "");
            }
            else if (SessionHelper.UserRoleGroup == PubEnums.RoleGroup.技术主管.ToString())
            {
                sb.Append(" and RoleGroup in (" + PubEnums.RoleGroup.技术主管.ToString("D") + "," + PubEnums.RoleGroup.技术员.ToString("D") + ")");
            }
            else
            {
                sb.Append(" and RoleGroup =" + PubEnums.RoleGroup.技术员.ToString("D") + "");
            }
            if (this.ddlUserName.SelectedValue.Trim().Length > 0 && this.ddlUserName.SelectedValue.Trim() != "-1")
            {
                sb.Append(" and  RealName like '%").Append(this.ddlUserName.SelectedValue.Trim()).Append("%'"); ;
            }

            if (ddl部门.SelectedValue != "-1")
            {
                sb.Append(" and  部门 = '").Append(ddl部门.SelectedValue).Append("'");
            }
            if (txt日期.Text.Trim().Length > 0)
            {
                sb.Append(" and  日期 >= '").Append(txt日期.Text.Trim() + " 00:00:00").Append("'");
                sb.Append(" and  日期 <= '").Append(txt日期.Text.Trim() + " 23:59:59").Append("'"); 
            }


            //sb.Append(" group by 日期,realname");
            //sb.Append(" order by 日期 desc");

            return sb.ToString();
           // return DataAccessHelp.GetDataTable(sb.ToString());

            //sb.Append(" 1=1 ");

            //if (SessionHelper.UserRoleGroup == PubEnums.RoleGroup.管理员.ToString())
            //{
            //    sb.Append(" and RoleGroup !=" + PubEnums.RoleGroup.客户.ToString("D") + "");
            //}
            //else if (SessionHelper.UserRoleGroup == PubEnums.RoleGroup.技术主管.ToString())
            //{
            //    sb.Append(" and RoleGroup in (" + PubEnums.RoleGroup.技术主管.ToString("D") + "," + PubEnums.RoleGroup.技术员.ToString("D") + ")");
            //}
            //else
            //{
            //    sb.Append(" and RoleGroup =" + PubEnums.RoleGroup.技术员.ToString("D") + "");
            //}

            //if (this.ddlUserName.SelectedValue.Trim().Length > 0 &&　this.ddlUserName.SelectedValue.Trim()!="-1")
            //{
            //    sb.Append(" and  RealName like '%").Append(this.ddlUserName.SelectedValue.Trim()).Append("%'"); ;
            //}

            //if (ddl部门.SelectedValue != "-1")
            //{
            //    sb.Append(" and  部门 = '").Append(ddl部门.SelectedValue).Append("'"); ;
            //}
            //if (txt日期.Text.Trim().Length > 0)
            //{
            //    sb.Append(" and  日期 = '").Append(txt日期.Text.Trim()).Append("'"); ;
            //}

            //return sb.ToString();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
           //if (this.txt日期.Text.Trim().Length==0)
           // {
           //     MessageboxInfo.MessageBox(this, MessageType.ERROR, "请输入日期");
           //     return;
           // }

            DataShow();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            DataShow();
        }

        protected void dgdaoim_sus_plyedr_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                if (e.CommandName.ToString() == "del")
                {
                    if (e.CommandArgument.ToString() != "")
                    {
                        if (new tb工作日志表Entity().Delete(int.Parse(e.CommandArgument.ToString())) > 0)
                        {
                            ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>ymPrompt.succeedInfo('删除成功。',300,180,'提示',null);</script>");
                            DataShow();
                        }
                        else
                        {
                            ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>ymPrompt.errorInfo('删除失败。',300,180,'提示',null);</script>");
                        }


                    }
                }
            }
            catch (APPException appex)
            {
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>ymPrompt.alert('" + appex.Message + "。',300,180,'提示',null);</script>");
            }
            catch (Exception ex)
            {
                log4netHelper.error("", ex);
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>ymPrompt.errorInfo('删除失败。',300,180,'提示',null);</script>");
            }
        }

        protected void dgdaoim_sus_plyedr_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex > -1)
            {
                //如果是绑定数据行 
                if (e.Row.RowType == DataControlRowType.DataRow)
                {


                    if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                    {
                        ((LinkButton)e.Row.Cells[10].FindControl("lbtnDelete")).Attributes.Add("onclick", "javascript:return confirm('你确认要删除吗?')");
                    }
                }
            }
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            Response.Redirect("WorkLogAdd.aspx", true);
        }
    }
}
